package com.showsu.editor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblUserLoginLogMapper;
import com.showsu.editor.entity.doo.UserLoginLog;
import com.showsu.editor.entity.po.TblUserLoginLog;
import com.showsu.editor.enums.DeleteStatusEnum;
import com.showsu.editor.service.IUserLoginLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: William
 * @Description:
 * @Date: Created in 2:45 下午 2023/3/23
 * @Modified By:
 */
@Service
@Slf4j
public class UserLoginLogServiceImpl extends ServiceImpl<TblUserLoginLogMapper, TblUserLoginLog> implements IUserLoginLogService {

    @Autowired
    private TblUserLoginLogMapper userLoginLogMapper;

    @Override
    public UserLoginLog save(Long userId, String sessionId, Integer loginType, Date loginTime, String clientIp, String clientOs, String clientBrowser, String clientOrigin) {
        TblUserLoginLog loginLog = new TblUserLoginLog();
        loginLog.setUserId(userId);
        loginLog.setSessionId(sessionId);
        loginLog.setLoginTime(loginTime);
        loginLog.setClientIp(clientIp);
        loginLog.setClientOs(clientOs);
        loginLog.setClientBrowser(clientBrowser);
        loginLog.setClientOrigin(clientOrigin);
        userLoginLogMapper.insert(loginLog);
        return BeanUtil.copyProperties(loginLog, UserLoginLog.class);
    }

    @Override
    public int logout(String sessionId) {
        LambdaUpdateWrapper<TblUserLoginLog> updateWrapper = new UpdateWrapper<TblUserLoginLog>().lambda()
                .eq(TblUserLoginLog::getSessionId, sessionId)
                .eq(TblUserLoginLog::getFlag, DeleteStatusEnum.Normal.getCode());
        TblUserLoginLog tblUserLoginLog = new TblUserLoginLog();
        tblUserLoginLog.setLogoutTime(new Date());
        return userLoginLogMapper.update(tblUserLoginLog, updateWrapper);
    }
}
